package com.bjt.wms.sys.controller;

import com.bjt.utils.IpUtil;
import com.bjt.utils.R;
import com.bjt.wms.sys.service.DataDicService;
import com.bjt.wms.sys.service.LogService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;

/**
 * @author WenbinJi
 * 用户登录接口
 */
public class LoginController extends Controller {
    @Inject
    private DataDicService dataDicService;

    @Inject
    private LogService logService;

    public void index() {
        //判断当前登录状态，若未登录
        render("/login.html");
    }

    /**
     * 用户登录
     */
    public void login() {
        //核对用户名|密码
        String username = getPara("username");
        String password = getPara("password");
        String ip = IpUtil.getIpAddr(getRequest());

        //核对验证码
        boolean result = validateCaptcha("captcha");
        if (!result) {
            logService.save(username, ip, false, "验证码错误");
            renderJson(R.error("验证码错误"));
            return;
        }

        if (username.equals("admin") && password.equals("123456")) {
            logService.save(username, ip, true);
            renderJson(R.ok().put("initData", dataDicService.getDataDicList()));
        } else {
            logService.save(username, ip, false, "用户名或密码错误");
            renderJson(R.error("用户名或密码错误"));
        }
    }

    /**
     * 生成验证码
     */
    public void captcha() {
        renderCaptcha();
    }
}
